import { Type } from "@nestjs/common";
import { ElasticsearchModuleOptions } from "@nestjs/elasticsearch";
import { ClassTransformOptions } from "class-transformer";
import { PostEntity } from "src/entity/Entity";

export type SearchType = 'like' | 'against' | 'elastic';
//接口
export interface ContentConfig {
    searchType?: SearchType;
}
export const content = (): ContentConfig => ({
    searchType: 'against',
});

declare type ClassToPlain<T> = { [key in keyof T]: T[key] };
export const elasticseach=():ElasticsearchModuleOptions=> ({
    node: 'http://localhost:9200',
    maxRetries: 10,
    requestTimeout: 60000,
    pingTimeout: 60000,
    sniffOnStart: true,
});

export type PostSearchBody=Pick<ClassToPlain<PostEntity>,"title"| 'body' | 'summary'>& {
    categories: string;
};
export enum OrderType {
    ASC = 'ASC',
    DESC = 'DESC',
}
export type OrderQueryType =
    | string
    | { name: string; order: `${OrderType}` }
    | Array<{ name: string; order: `${OrderType}` } | string>;

export enum TreeChildrenResolve {
    DELETE = 'delete',
    UP = 'up',
    ROOT = 'root',
}
export enum SelectTrashMode {
    ALL = 'all', // 包含已软删除和未软删除的数据
    ONLY = 'only', // 只包含软删除的数据
    NONE = 'none', // 只包含未软删除的数据
}

export const CRUD_OPTIONS = 'crud_options';

/**
 * CURD控制器方法列表
 */
export type CurdMethod = 'detail' | 'delete' | 'restore' | 'list' | 'store' | 'update';

/**
 * CRUD装饰器的方法选项
 */
export interface CrudMethodOption {
    /**
     * 序列化选项,如果为`noGroup`则不传参数，否则根据`id`+方法匹配来传参
     */
    serialize?: ClassTransformOptions | 'noGroup';
    hook?: (target: Type<any>, method: string) => void;
}
/**
 * 每个启用方法的配置
 */
export interface CurdItem {
    name: CurdMethod;
    option?: CrudMethodOption;
}

/**
 * CRUD装饰器选项
 */
export interface CurdOptions {
    id: string;
    // 需要启用的方法
    enabled: Array<CurdMethod | CurdItem>;
    // 一些方法要使用到的自定义DTO
    dtos: {
        [key in 'list' | 'store' | 'update']?: Type<any>;
    };
}